  <template>
      <div class="edit-popup">
          <popup
              ref="popupRef"
              :title="popupTitle"
              :async="true"
              width="75%"
              :clickModalClose="true"
              @confirm="handleSubmit"
              @close="handleClose"
          >
              <el-form
                  ref="formRef"
                  :model="formData"
                  label-width="200px"
                  :rules="formRules"
                  :inline="true"
                  class="remindFormEdit"
                  :disabled="disabled"
              >
                  <el-form-item label="提醒时间" prop="remindTime">
                      <el-date-picker
                          class="flex-1 !flex"
                          v-model="formData.remindTime"
                          type="datetime"
                          clearable
                          value-format="YYYY-MM-DD hh:mm:ss"
                          placeholder="请选择提醒时间"
                      />
                  </el-form-item>

                  <el-form-item label="提醒结束时间" prop="remindEndTime">
                      <el-date-picker
                          class="flex-1 !flex"
                          v-model="formData.remindEndTime"
                          type="datetime"
                          clearable
                          value-format="YYYY-MM-DD hh:mm:ss"
                          placeholder="请选择提醒结束时间"
                      />
                  </el-form-item>
                  <el-form-item label="提醒内容" prop="remindContent">
                      <el-input
                          v-model="formData.remindContent"
                          placeholder="请输入提醒内容"
                          type="textarea"
                          :autosize="{ minRows: 4, maxRows: 6 }"
                      />
                  </el-form-item>
              </el-form>
          </popup>
      </div>
  </template>
  <script lang="ts" setup>
  import type { FormInstance } from 'element-plus'
  import { remindEdit, remindAdd, remindDetail } from '@/api/remind'
  import Popup from '@/components/popup/index.vue'
  import feedback from '@/utils/feedback'
  import type { PropType } from 'vue'
  defineProps({
      dictData: {
          type: Object as PropType<Record<string, any[]>>,
          default: () => ({})
      }
  })
  const emit = defineEmits(['success', 'close'])
  const formRef = shallowRef<FormInstance>()
  const popupRef = shallowRef<InstanceType<typeof Popup>>()
  const mode = ref('add')
  const popupTitle = computed(() => {
      return mode.value == 'edit' ? '编辑提醒' : '新增提醒'
  })

  const formData = reactive({
      id: '',
      remindTime: '',
      remindContent: '',
      remindEndTime: ''
  })

  const formRules = {
      remindTime: [
          {
              required: true,
              message: '请选择提醒时间',
              trigger: ['blur']
          }
      ],
      remindContent: [
          {
              required: true,
              message: '请输入提醒内容',
              trigger: ['blur']
          }
      ],
      remindEndTime: [
          {
              required: true,
              message: '请选择提醒结束时间',
              trigger: ['blur']
          }
      ]
  }

  const handleSubmit = async () => {
      await formRef.value?.validate()
      const data: any = { ...formData }
      mode.value == 'edit' ? await remindEdit(data) : await remindAdd(data)
      popupRef.value?.close()
      feedback.msgSuccess('操作成功')
      emit('success')
  }

  const open = (type = 'add') => {
      mode.value = type
      popupRef.value?.open()
  }

  const setFormData = async (data: Record<string, any>) => {
      for (const key in formData) {
          if (data[key] != null && data[key] != undefined) {
              //@ts-ignore
              formData[key] = data[key]
          }
      }
  }

  const getDetail = async (row: Record<string, any>) => {
      const data = await remindDetail({
          id: row.id
      })
      setFormData(data)
  }

  const handleClose = () => {
      emit('close')
  }

  defineExpose({
      open,
      setFormData,
      getDetail
  })
  </script>

  <style scoped>
  .remindFormEdit .el-form-item {
      width: 500px;
  }
  ::v-deep .el-collapse-item__header {
    padding-left: 54px;
    background-color: var(--el-bg-color-collapse);
    font-size: 16px;
    font-weight: 600;
    color: var(--el-text-color-collapse);
  }
  ::v-deep .el-collapse-item__wrap {
      padding-top: 40px;
  }
  </style>
